// pages/taskFail/taskFail.js
import api from "../../config/api";
import { shareOptions } from "../../utils/access";
import request from "../../utils/request";
import { getBarAndTitleHeight, getXsScreen, stringify, getStorage } from "../../utils/utils"
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    userUrl:'',
    day:'',
    taskData:{},
    // 信号栏高度
    ...getBarAndTitleHeight(),
    // 是否是小屏幕尺寸
		...getXsScreen(),
  },
  // 加载loading
  loading: false,
  // 获取用户信息
  async getUserData(){
    const userInfo = getStorage("userInfo")
    const userId = getStorage("userId")
    const { data } = await request.get(api.getDefaultData,{ userId })
    this.setData({
      userName:userInfo.nickName,
      userUrl:userInfo.avatarUrl,
      day:data.totalClassifyNum
    })
  },
  // 跳转桶内详情
  goBucketAnalyse(){
    console.log(123)
    const { taskData } = this.data
    wx.navigateTo({
      url: `/pages/bucketAnalyse/bucketAnalyse?ingredient=${(taskData.ingredient)}&bucketType=${taskData.bucketType}&url=${taskData.url}&taskData=${stringify(taskData)}`,
    })
  },
  // 申诉弹窗显示
  shensu(){
    this.modal.show()
  },
  // 提交申诉
  async appeal() {
		if (this.loading) return
		this.loading = true
		this.modal.hide()
		const { taskData } = this.data
		const params = {
			userDayTaskId: taskData.taskId,
			picUrl: taskData.url,
			recognitionResultType: taskData.goodsType,
			recognitionResult: taskData.goodsName
		}
		console.log('申诉参数：', params)
		const res = await request.post(api.taskAppeal, params, { showModal: false })
		console.log('申诉接口返回：', params)
		if (res?.httpStatus === 200) {
			wx.showToast({
				title: '提交成功',
				content: '申诉通过后任务会自动完成，奖励也会补发'
			})
		} else {
			wx.showToast({
				title: '提交失败',
				content: '任务申诉已通过，请不要重复提交\n网络不好，请重新拍照'
			})
		}
		setTimeout(() => {
			this.loading = false
			wx.navigateBack({
				delta: 2,
			})
		}, 800)
  },
  // 返回上一页
  backTo(){
    console.log(111)
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserData()
    this.setData({
      taskData:options
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.modal = this.selectComponent('#modal')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
		return shareOptions('default', e)
	},
})